﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;

namespace WinForm {
    public partial class FormDB : Form {
        BLL.BakupAndRestoreDb bll = new BLL.BakupAndRestoreDb();
        protected string backupPath = "", restorePath = "";
        public FormDB() {
            InitializeComponent();
        }

        private void btnBackup_Click(object sender, EventArgs e) {
            if (txtBackup.Text == "") {
                MessageBox.Show("请选择有效的备份路径。", "提示");
                return;
            }
            try {
                this.Cursor = Cursors.WaitCursor;
                if (File.Exists(backupPath)) {
                    File.Delete(backupPath);
                }
                bll.BakupDb(backupPath);
                MessageBox.Show("数据库备份成功。");
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
            finally {

                Cursor = Cursors.Default;
            }
        }

        private void btnRestorePath_Click(object sender, EventArgs e) {
            ofdlgRestore.FilterIndex = 1;
            ofdlgRestore.FileName = "";
            ofdlgRestore.Filter = "bak Files (*.bak) | *.bak";
            if (ofdlgRestore.ShowDialog() == DialogResult.OK) {
                txtRestore.Text = ofdlgRestore.FileName;
            }
            restorePath = txtRestore.Text;
        }

        private void btnRestore_Click(object sender, EventArgs e) {
            Cursor = Cursors.WaitCursor;
            if (restorePath == "") {
                MessageBox.Show("请选择有效的数据库备份文件。", "提示");
                return;
            }
            try {
                bll.RestoreDb(restorePath);
                MessageBox.Show("恢复成功。");
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
            finally {
                this.Cursor = Cursors.Default;
            }

        }

        private void btnBackupPath_Click(object sender, EventArgs e) {
            sfdlgBackup.FilterIndex = 1;
            sfdlgBackup.FileName = "";
            sfdlgBackup.Filter = "Bak files (*.bak) | *.bak";
            if (sfdlgBackup.ShowDialog() == DialogResult.OK) {
                txtBackup.Text = sfdlgBackup.FileName;
            }
            backupPath = txtBackup.Text;
        }
    }
}